import { execute } from "../../database/connector";

import { MachineAppointmentQueries } from "../queries/machine-appointment.query";
import { IMachineAppointment } from "../../models/machine-appointment.model";

/**
 * gets active machine appointments
 */
export const getMachineAppointments = async () => {
  return execute<IMachineAppointment[]>(MachineAppointmentQueries.GetMachineAppointments, []);
};

/**
 * gets applied machine appointments
 */
export const getAppliedMachineAppointmentsById = async (id: IMachineAppointment['id']) => {
  return execute<IMachineAppointment[]>(MachineAppointmentQueries.GetAppliedMachineAppointmentsById, [id]);
};

/**
 * gets a machine appointment based on id provided
 */
export const getMachineAppointmentById = async (id: IMachineAppointment['id']) => {
  return execute<IMachineAppointment>(MachineAppointmentQueries.GetMachineAppointmentById, [id]);
};

/**
 * adds a new active machine appointment record
 */
export const insertMachineAppointment = async (machineAppointment: IMachineAppointment) => {
  const result = await execute<{ affectedRows: number }>(MachineAppointmentQueries.AddMachineAppointment, [
    [Object.values(machineAppointment)]
  ]);
  return result.affectedRows > 0;
};

/**
 * updates machine appointment information based on the id provided
 */
export const updateMachineAppointment = async (machineAppointment: IMachineAppointment) => {
  const result = await execute<{ affectedRows: number }>(MachineAppointmentQueries.UpdateMachineAppointmentById, [
    machineAppointment,
    machineAppointment.id
  ]);
  return result.affectedRows > 0;
};

/**
 * updates machine appointment information based on the id provided
 */
 export const deleteMachineAppointment = async (id: IMachineAppointment['id']) => {
  const result = await execute<{ affectedRows: number }>(MachineAppointmentQueries.DeleteMachineAppointmentById, [
    id
  ]);
  return result.affectedRows > 0;
};